package com.handu.luna.boss.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Random;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.handu.luna.core.domain.goods.Album;
import com.handu.luna.core.domain.goods.Goods;
import com.handu.luna.core.domain.support.FileUploadSupport;
import com.handu.luna.core.domain.support.FileUploadSupport.ImageType;


@Controller
@RequestMapping("/goods/album*")
public class AlbumController extends BaseController{
	
	/**
	 * 商品相册
	 * @param 
	 * @return
	 */
	@RequestMapping(value ="index/{id}",method = RequestMethod.GET)
	public BossModel index(@PathVariable long id){
		return createModel("/goods/goods/album/index").add("albums",context.getAlbumRepository().findByGoodsId(id)).add("goodId",id);
	}
	
	/**
	 * 商品相册
	 * @param 
	 * @return
	 */
	@RequestMapping(value ="list/{goodId}")
	public BossModel list(@PathVariable long goodId){
		return createModel("/goods/goods/album/index").add("albums",context.getAlbumRepository().findByGoodsId(goodId)).add("goodId",goodId);
	}

	
	/**
	 * 删除功能
	 * @return
	 */
	
	@RequestMapping(value="delete",method= RequestMethod.POST)
	public BossModel delete(long id,long goodId){
		
		context.getAlbumRepository().delete(id);;
		
		return createModel("goods/goods/album/album_table").add("albums",context.getAlbumRepository().findByGoodsId(goodId));
	}
	
	/**
	 * 跳转添加图片页面
	 * @return
	 */
	@RequestMapping(value="add/{goodId}")
	public BossModel add(@PathVariable long goodId){
		return createModel("goods/goods/album/info").add("albums",context.getAlbumRepository().findByGoodsId(goodId)).add("goodId",goodId);
	}
	
	@RequestMapping(value="upload",method=RequestMethod.POST)
	public BossModel  upload(@RequestParam MultipartFile files,String goodsid){
        if (!files.isEmpty()) {
            	/*命名规则*/
            	String pic_type = files.getContentType();
            	String fileName;
            	Random random = new Random();
    			int x = random.nextInt(899999);
    			int randomValue = x+100000;
    			Goods goods = context.getGoodsRepository().findOne(Long.valueOf(goodsid));
            	String file_ture_name = goods.getGoodsSN()+"_"+new Date().getTime()+randomValue;
            	if(pic_type.equals("image/jpeg")){
            		fileName =   file_ture_name.concat(".jpg");
            	} else if (pic_type.equals("image/png")){
            		fileName = file_ture_name.concat(".png");
            	} else if(pic_type.equals("image/bmp")){
            		fileName =  file_ture_name.concat(".bmp");
            	} else if(pic_type.equals("image/gif")){
            		fileName = file_ture_name.concat(".gif");
            	} else	{ 
            		fileName = file_ture_name.concat(".jpg");
            	}
            	
            	try {
					String returnMessage = FileUploadSupport.upload(files.getInputStream(), fileName, ImageType.GOODS);
					Album album = new Album();
					album.setGoodId(Long.valueOf(goodsid));
					album.setPicAddress(returnMessage);
					context.getAlbumRepository().save(album);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
        }
        return null;
	}
	


}
